/*
 * 
 */
package com.yuanmei.stockeyes.charts;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnTouchListener;

import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;
import com.yuanmei.stockeyes.R;
import com.yuanmei.stockeyes.artifacts.Extensions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.limc.androidcharts.entity.DateValueEntity;
import cn.limc.androidcharts.entity.LineEntity;
import cn.limc.androidcharts.event.IDisplayCursorListener;
import cn.limc.androidcharts.view.SlipMinusLineChart;


public class TalibTsfChart implements ITalibChart {
    private final Core mTaLibCore = new Core();
    private Context mContext;
    private SlipMinusLineChart  mTsfChartView;
    private static final String TAG = TalibTsfChart.class.getName();

    private List<DateValueEntity> mTsfDateValueList;
    private LineEntity<DateValueEntity> mTsfLineEntity;

    private int    mTsfPeriod = 5;
    private String mTsfPeriodName;

    private int mTaIdxDataSetsBegin;
    private int mTaIdxDataSetsNumber;

    public TalibTsfChart(Context context) {
        mTsfChartView = new SlipMinusLineChart(context);
        TalibChartFactory.commonInitGridChart(mTsfChartView);
        TalibChartFactory.commonInitSlipLineChart(mTsfChartView);

        mContext       =  mTsfChartView.getContext();
        mTsfPeriodName = mContext.getString(R.string.param_natr_period);
    }

    @Override
    public List<Map<String, Object>> getTaParamsList() {
        List<Map<String, Object>> outParamsList = new ArrayList<Map<String, Object>>();

        Map<String, Object> item = new HashMap<String, Object>();
        item.put(ITalibChart.ITaParamNameKey,  mTsfPeriodName);
        item.put(ITalibChart.ITaParamValueKey, mTsfPeriod);
        outParamsList.add(item);

        return outParamsList;
    }

    @Override
    public void setTaParams(Hashtable<String, Integer> inParamMaps) {
        if (inParamMaps.containsKey(mTsfPeriodName)
                ) {
            mTsfPeriod = inParamMaps.get(mTsfPeriodName);
        }
    }

    @Override
    public boolean reloadData(int dataLen,
                              float[] openPrices,
                              float[] highPrices,
                              float[] lowPrices,
                              float[] closePrices,
                              float[] volumes,
                              float[] amounts,
                              int[] dates) {
        MInteger outBegIdx    = new MInteger();
        MInteger outNBElement = new MInteger();
        double[] outTsf       = new double[dataLen];

        RetCode retCode = mTaLibCore.tsf(0, closePrices.length - 1,
                closePrices,
                mTsfPeriod,
                outBegIdx, outNBElement, outTsf);
        if (retCode != RetCode.Success) {
            Log.e(TAG, "NATR calc error retCode=" + retCode);
            return false;
        }

        mTsfDateValueList = new ArrayList<>();
        for (int i = 0; i < dataLen; i++) {
            if (i < outBegIdx.value) {
                mTsfDateValueList.add(new DateValueEntity(
                        (float) (outTsf[0]), dates[i]));
            } else {
                int idx = i - outBegIdx.value;
                mTsfDateValueList.add(new DateValueEntity(
                        (float) (outTsf[idx]), dates[i]));
            }
        }

        List<LineEntity<DateValueEntity>> lines = new ArrayList<LineEntity<DateValueEntity>>();

        mTsfLineEntity = new LineEntity<DateValueEntity>();
        mTsfLineEntity.setTitle("TSF");
        mTsfLineEntity.setLineColor(Color.WHITE);
        mTsfLineEntity.setLineData(mTsfDateValueList);
        lines.add(mTsfLineEntity);

        mTsfChartView.setLinesData(lines);

        mTaIdxDataSetsBegin  = outBegIdx.value;
        mTaIdxDataSetsNumber = outNBElement.value;
        return true;
    }

    @Override
    public int getTaIndexNumber() {
        return mTaIdxDataSetsNumber;
    }

    @Override
    public String getTaIndexParms() {
        return mTsfPeriod + "" ;
    }

    @Override
    public String getTaIndexShortName() {
        return "TSF";
    }

    @Override
    public Spanned getTaIndexDetail() {
        if (null == mTsfDateValueList) {
            return new SpannableString("----");
        }
        int index = mTsfChartView.getSelectedIndex();

        DateValueEntity tsfEntity = mTsfDateValueList.get(index);
        String          tsfText   = String.format(Locale.ENGLISH, "TSF:%.2f ",
                tsfEntity.getValue());
        String          dateText  = String.format(Locale.ENGLISH, "%d ",
                tsfEntity.getDate());

        String    finalString = tsfText + dateText;
        Spannable sb          = new SpannableString(finalString);

        sb.setSpan(new ForegroundColorSpan(mTsfLineEntity.getLineColor()), finalString.indexOf(tsfText),
                finalString.indexOf(tsfText) + tsfText.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        sb.setSpan(new ForegroundColorSpan(Color.GRAY), finalString.indexOf(dateText),
                finalString.indexOf(dateText) + dateText.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

        return (sb);
    }

    @Override
    public int getTaIndexBegin() {
        return mTaIdxDataSetsBegin;
    }

    @Override
    public View getChartView() {
        return mTsfChartView;
    }

    @Override
    public void setDisplayFrom(int from) {
        mTsfChartView.setDisplayFrom(from);
    }

    @Override
    public void setDisplayCrossXOnTouch(boolean displayCrossXOnTouch) {
        mTsfChartView.setDisplayCrossXOnTouch(displayCrossXOnTouch);
    }

    @Override
    public void setDisplayNumber(int number) {
        mTsfChartView.setDisplayNumber(number);
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        mTsfChartView.setOnTouchListener(l);
    }

    @Override
    public void setOnDisplayCursorListener(
            IDisplayCursorListener l) {
        mTsfChartView.setOnDisplayCursorListener(l);
    }

    @Override
    public PointF getTouchPoint() {
        return mTsfChartView.getTouchPoint();
    }

    @Override
    public void setTouchPoint(PointF touchPoint) {
        mTsfChartView.setTouchPoint(touchPoint);
    }

    @Override
    public void postInvalidate() {
        mTsfChartView.postInvalidate();
    }

    @Override
    public float getValidMiddleY() {
        return mTsfChartView.getValidMiddleY();
    }
}
